package com.smq.chatserver.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smq.chatserver.bean.Teem;
import com.smq.chatserver.bean.TeemMember;
import com.smq.chatserver.mapper.TeemMapper;
import com.smq.chatserver.mapper.TeemMemberMapper;
import com.smq.chatserver.services.TeemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class TeemServiceImpl extends ServiceImpl<TeemMapper, Teem> implements TeemService {

    @Autowired
    TeemMapper teemMapper;

    @Autowired
    TeemMemberMapper teemMemberMapper;

    @Override
    public List<Teem> query(String userId) {
        ArrayList<Teem> teems = new ArrayList<>();
        List<TeemMember> teemMembers = teemMemberMapper.selectList(new QueryWrapper<TeemMember>().eq("user_id", userId));
        for (TeemMember teemMember : teemMembers) {
            String teemId = teemMember.getTeemId();
            teems.add(teemMapper.selectOne(new QueryWrapper<Teem>().eq("id", teemId)));
        }
        return teems;
    }

    @Override
    public Teem create(String name, String iconUri, String userId) {
        Teem teem = new Teem(UUID.randomUUID().toString(), userId, iconUri, name, 0, System.currentTimeMillis(), null, null);
        teemMapper.insert(teem);
        return teem;
    }
}
